package com.rede.didiok.rank.restapi;

import javax.annotation.Resource;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.rede.didiok.common.annotion.AvoidRepeatableCommit.AvoidRepeatableCommit;
import com.rede.didiok.common.annotion.log.BussinessLog;
import com.rede.didiok.common.constant.conf.SysConf;
import com.rede.didiok.common.dto.rank.RankPraiseRecordDto;
import com.rede.didiok.common.enums.BehaviorEnum;
import com.rede.didiok.common.exception.ThrowableUtils;
import com.rede.didiok.common.utils.ResultUtil;
import com.rede.didiok.common.validator.group.Default;
import com.rede.didiok.common.validator.group.Insert;
import com.rede.didiok.rank.service.RankPraiseRecordService;

/**
 * 点赞模块
 *
 * @author 遇见
 */
@RestController
@RequestMapping("/rank/praise")
@Api(value = "点赞模块接口", tags = {"点赞模块接口"})
@Slf4j
public class RankPraiseApi {

    @Resource
    RankPraiseRecordService rankPraiseRecordService;

    @ApiOperation(value = "检查是否点赞/踩", notes = "检查是否点赞")
    @PostMapping("/checkPraise")
    public String checkPraise(@Validated({Insert.class}) @RequestBody RankPraiseRecordDto communityPraiseRecordDto, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("检查是否点赞");
        return ResultUtil.result(SysConf.SUCCESS, rankPraiseRecordService.hasPraised(communityPraiseRecordDto));
    }

    @ApiOperation(value = "获取点赞/踩数", notes = "点赞/踩数")
    @PostMapping("/getPraiseCount")
    public String getPraiseCount(@Validated({Insert.class}) @RequestBody RankPraiseRecordDto rankPraiseRecordDto, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("检查是否点赞");
        return ResultUtil.result(SysConf.SUCCESS, rankPraiseRecordService.praisedCount(rankPraiseRecordDto));
    }

    /**
     * 点赞
     *
     * @return
     */
    @AvoidRepeatableCommit(timeout = 2000)
    @BussinessLog(value = "点赞", behavior = BehaviorEnum.RANK_PRAISE)
    @ApiOperation(value = "点赞", notes = "点赞指定资源", response = String.class)
    @PostMapping("/praise")
    public String confirm(@Validated({Insert.class}) @RequestBody RankPraiseRecordDto rankPraiseRecordDto, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("点赞");
        return rankPraiseRecordService.praise(rankPraiseRecordDto);
    }

    /**
     * 取消点赞
     *
     * @return
     */
    @AvoidRepeatableCommit(timeout = 2000)
    @BussinessLog(value = "取消点赞", behavior = BehaviorEnum.CANCEL_RANK_PRAISE)
    @ApiOperation(value = "取消点赞", notes = "取消点赞", response = String.class)
    @PostMapping("/cancelPraise")
    public String cancelPraise(@Validated({Default.class}) @RequestBody RankPraiseRecordDto rankPraiseRecordDto, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("取消点赞");
        return rankPraiseRecordService.cancelPraise(rankPraiseRecordDto);
    }

    /**
     * 点踩
     *
     * @return
     */
    @AvoidRepeatableCommit(timeout = 2000)
    @BussinessLog(value = "点踩", behavior = BehaviorEnum.RANK_TREAD)
    @ApiOperation(value = "点踩", notes = "点赞指定资源", response = String.class)
    @PostMapping("/tread")
    public String tread(@Validated({Insert.class}) @RequestBody RankPraiseRecordDto rankPraiseRecordDto, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("点踩");
        return rankPraiseRecordService.tread(rankPraiseRecordDto);
    }

    /**
     * 取消点赞
     *
     * @return
     */
    @AvoidRepeatableCommit(timeout = 2000)
    @BussinessLog(value = "取消点踩", behavior = BehaviorEnum.CANCEL_RANK_TREAD)
    @ApiOperation(value = "取消点踩", notes = "取消点赞", response = String.class)
    @PostMapping("/cancelTread")
    public String cancelTread(@Validated({Default.class}) @RequestBody RankPraiseRecordDto rankPraiseRecordDto, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("取消点踩");
        return rankPraiseRecordService.cancelTread(rankPraiseRecordDto);
    }


    @ApiOperation(value = "获取点赞用户列表", notes = "获取点赞用户列表", response = String.class)
    @PostMapping("/getPraiseUserList")
    public String getPraiseUserList(@Validated({Default.class}) @RequestBody RankPraiseRecordDto rankPraiseRecordDto, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取点赞用户列表");
        return ResultUtil.successWithData(rankPraiseRecordService.getPraiseList(rankPraiseRecordDto));
    }
}
